﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;

namespace 玩具商城管理系统.Order
{
    public partial class trouble : Form
    {
        public trouble()
        {
            InitializeComponent();
        }
        SqlDataAdapter dahot, datime, daqr;
        DataSet ds = new DataSet();
        void init()
        {
            string str = "SELECT sort_name as 类别名称, COUNT(*) AS 下单数 FROM hot h join commodity c  on  h.hot_commodityname =c.commodity_name  where h.hot_status = '已审核'GROUP BY sort_name;";
            string sdr = "SELECT \r\n    CONCAT(\r\n        DATEPART(YEAR, orderT_time), '年', \r\n        CASE \r\n            WHEN DATEPART(QUARTER, orderT_time) = 1 THEN '第1季度'\r\n            WHEN DATEPART(QUARTER, orderT_time) = 2 THEN '第2季度'\r\n            WHEN DATEPART(QUARTER, orderT_time) = 3 THEN '第3季度'\r\n            WHEN DATEPART(QUARTER, orderT_time) = 4 THEN '第4季度'\r\n        END\r\n    ) AS 年份季度,\r\n    SUM(profit) AS 订单总额\r\nFROM \r\n    orderT\r\nWHERE \r\n    orderT_check = '已审核'\r\nGROUP BY \r\n    DATEPART(YEAR, orderT_time), \r\n    DATEPART(QUARTER, orderT_time)\r\nORDER BY \r\n    年份季度;";
            string sqr = "SELECT  hot_commodityname as 商品名称,count(hot_commoditynum) AS 销量 FROM hot WHERE hot_status = '已审核' GROUP BY hot_commodityname ORDER BY 销量 ASC; ";
            dahot = new SqlDataAdapter(str, DB.cn);
            dahot.Fill(ds, "Proform_info");
            datime = new SqlDataAdapter(sdr, DB.cn);
            datime.Fill(ds, "datime_info");
            daqr = new SqlDataAdapter(sqr, DB.cn);
            daqr.Fill(ds, "daqr_info");

        }
        void showAll()
        {
            DataView dvProform = new DataView(ds.Tables["Proform_info"]);
            dgvorder.DataSource = dvProform;
            DataView dvEmployee = new DataView(ds.Tables["datime_info"]);
            dgvdatime.DataSource = dvEmployee;
            DataView dvqr = new DataView(ds.Tables["daqr_info"]);
            dgvqr.DataSource = dvqr;
        }
  

        private void timer1_Tick(object sender, EventArgs e)
        {
          
        }

        private void label1_Click(object sender, EventArgs e)
        {
            label1.ForeColor = Color.Blue;
            label2.ForeColor = Color.Black;
            label3.ForeColor = Color.Black;
            groupBox1.Visible = true;
            groupBox2.Visible = false;
            groupBox3.Visible = false;
        }

        private void label2_Click(object sender, EventArgs e)
        {
            label1.ForeColor = Color.Black;
            label2.ForeColor = Color.Blue;
            label3.ForeColor = Color.Black;
            groupBox1.Visible = false;
            groupBox2.Visible = true;
            groupBox3.Visible = false;
        }

        private void label3_Click(object sender, EventArgs e)
        {
            label1.ForeColor = Color.Black;
            label2.ForeColor = Color.Black;
            label3.ForeColor = Color.Blue;
            groupBox1.Visible = false;
            groupBox2.Visible = false;
            groupBox3.Visible = true;
        }

        private void trouble_Load(object sender, EventArgs e)
        {
            init();
            showAll();

            //饼图
            string[] Xname = new string[ds.Tables["Proform_info"].Rows.Count];
            double[] Yprofit = new double[ds.Tables["Proform_info"].Rows.Count];
            for (int i = 0; i < ds.Tables["Proform_info"].Rows.Count; i++)
            {
                Xname[i] = ds.Tables["Proform_info"].Rows[i][0].ToString();
                Yprofit[i] = double.Parse(ds.Tables["Proform_info"].Rows[i][1].ToString());
            }
            chart1.Series[0].Points.DataBindXY(Xname, Yprofit);
            chart1.Series[0]["PieLabelStyle"] = "Outside";

            //折线图
            string[] Xname2 = new string[ds.Tables["datime_info"].Rows.Count];
            double[] Yprofit2 = new double[ds.Tables["datime_info"].Rows.Count];
            for (int i = 0; i < ds.Tables["datime_info"].Rows.Count; i++)
            {
                Xname2[i] = ds.Tables["datime_info"].Rows[i][0].ToString();
                Yprofit2[i] = double.Parse(ds.Tables["datime_info"].Rows[i][1].ToString());
            }
            chart2.ChartAreas[0].AxisX.LabelStyle.Font = new Font("Arial", 9);
            chart2.ChartAreas[0].AxisX.Interval = 1;
            chart2.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
            chart2.Series[0].Points.DataBindXY(Xname2, Yprofit2);

            //柱形图
            string[] Xname3 = new string[ds.Tables["daqr_info"].Rows.Count];
            double[] Yprofit3 = new double[ds.Tables["daqr_info"].Rows.Count];
            for (int i = 0; i < ds.Tables["daqr_info"].Rows.Count; i++)
            {
                Xname3[i] = ds.Tables["daqr_info"].Rows[i][0].ToString();
                Yprofit3[i] = double.Parse(ds.Tables["daqr_info"].Rows[i][1].ToString());
            }
            chart3.Series[0].Points.DataBindXY(Xname3, Yprofit3);
            chart3.Series[0]["PieLabelStyle"] = "Outside";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
